package com.gensdai.leliang.dialog;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.gensdai.leliang.R;
import com.gensdai.leliang.popupwindow.BasePopupWindow;
import com.gensdai.leliang.view.Toaster;
import com.hp.hpl.sparta.Text;

import butterknife.BindView;

/**
 * Created by zsxx on 2017/7/28.
 */

public class FilterMultPopup extends BasePopupWindow {


    @BindView(R.id.isnewProductCheck)
    CheckBox isnewProductCheck;
    @BindView(R.id.lowPrice)
    EditText lowPrice;
    @BindView(R.id.hightPrice)
    EditText hightPrice;
    @BindView(R.id.reset)
    Button reset;
    @BindView(R.id.done)
    Button done;
    private View popupView;


    OnBtnClickListener click;

    public FilterMultPopup(Activity context) {
        super(context);


    }

    public void setOnBtnClickListener(OnBtnClickListener click) {
        this.click = click;
    }


    public void init() {
        isnewProductCheck = (CheckBox) popupView.findViewById(R.id.isnewProductCheck);
        lowPrice = (EditText) popupView.findViewById(R.id.lowPrice);
        hightPrice = (EditText) popupView.findViewById(R.id.hightPrice);
        reset = (Button) popupView.findViewById(R.id.reset);
        done = (Button) popupView.findViewById(R.id.done);

        lowPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
//                if(TextUtils.isEmpty(s.toString())){
//                    return;
//                }
//                String places = "0";
//                if(s.toString().startsWith(".")){
//                    places = "0" + s;
//                }else{
//                    places = s.toString();
//                }
//                String hightp = hightPrice.getText().toString().toString();
//                if(!TextUtils.isEmpty(hightp)){
//                    //格式化下价格
//                    if(hightp.toString().startsWith(".")){
//                        hightp = "0" + hightp;
//                    }
//
//                    double low = Double.valueOf(TextUtils.isEmpty(places)?"0":places);
//                    double hig = Double.valueOf(TextUtils.isEmpty(hightp)?"0":hightp);
//
//                    if(low>=hig){
//                        Toaster.showOneToast("请输入小于" + hig+"的价格");
//                        lowPrice.setText("");
//                    }
//                }
            }
        });
        hightPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
//                if(TextUtils.isEmpty(s.toString())){
//                    return;
//                }
//                String places = "0";
//                if(s.toString().startsWith(".")){
//                    places = "0" + s;
//                }else{
//                    places = s.toString();
//                }
//                String lowp = lowPrice.getText().toString().toString();
//                if(!TextUtils.isEmpty(lowp)){
//                    //格式化下价格
//                    if(lowp.toString().startsWith(".")){
//                        lowp = "0" + lowp;
//                    }
//
//                    double low = Double.valueOf(TextUtils.isEmpty(lowp)?"0":lowp);
//                    double hig = Double.valueOf(TextUtils.isEmpty(places)?"0":places);
//
//                    if(hig <= low){
//                        Toaster.showOneToast("请输入大于" + low+"的价格");
//                        hightPrice.setText("");
//                    }
//                }
            }
        });


        done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click != null) {
                    String low = lowPrice.getText().toString().trim();
                    String hig = hightPrice.getText().toString().trim();


                    if (!TextUtils.isEmpty(low)) {
                        if (low.startsWith(".")) {
                            low = "0" + low;
                        }
                    } else {
                        low = "";
                    }
                    if (!TextUtils.isEmpty(hig)) {
                        if (hig.startsWith(".")) {
                            hig = "0" + hig;
                        }
                    } else {
                        hig = "";
                    }

                    double l = Double.valueOf(TextUtils.isEmpty(low) ? "0" : low);
                    double h = Double.valueOf(TextUtils.isEmpty(hig) ? "0" : hig);
                    if (!TextUtils.isEmpty(low) && TextUtils.isEmpty(hig) || !TextUtils.isEmpty(hig) && TextUtils.isEmpty(low)) {
                        click.onDoneClick(isnewProductCheck.isChecked(), low, hig, FilterMultPopup.this);
                        lowPrice.setText(low);
                        hightPrice.setText(hig);
                    } else if (!TextUtils.isEmpty(low) && !TextUtils.isEmpty(hig)) {
                        if (l > h) {
                            click.onDoneClick(isnewProductCheck.isChecked(), hig, low, FilterMultPopup.this);
                            lowPrice.setText(hig);
                            hightPrice.setText(low);
                        } else {
                            click.onDoneClick(isnewProductCheck.isChecked(), low, hig, FilterMultPopup.this);
                        }
                    } else {
                        click.onDoneClick(isnewProductCheck.isChecked(), hig, low, FilterMultPopup.this);
                    }

                }
            }
        });

        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lowPrice.setText("");
                hightPrice.setText("");
                isnewProductCheck.setChecked(false);
            }
        });
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow_filter, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.filter);
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(-(250 * 5), 0, 260);
    }

    @Override
    protected Animation initExitAnimation() {
        return getTranslateAnimation(0, -(250 * 5), 260);
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }


    public interface OnBtnClickListener {
        void onDoneClick(boolean isnew, String lowprice, String hightprice, BasePopupWindow p);
    }

}
